package com.album.provider;

 

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.graphics.Bitmap;

import android.net.Uri; 
public class AlbumProvider extends ContentProvider {
	private static final String TAG = "AlbumProvider";
	private static SQLiteDatabase db;
	private DatabaseHelper mOpenHelper;
	private static final String DATABASE_NAME = "album.db";
	private static final int SELLING_INFO = 1;
	private static final int ITEM_TB_NAME_APPLICATION_INFO = 3;
	
	private static final int PIC_INFO = 2;
	private static final int ITEM_TB_NAME_PIC_INFO = 4;
	
	private static final int PIC_INFO_d = 5;
	private static final int ITEM_TB_NAME__INFO = 6;

	private static final int DATABASE_VERSION = 1;
	private static final UriMatcher sUriMatcher;
	SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
	/*
	 * 打开数据库 
	 */
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AlbumPad.AUTHORITY,
				AlbumPad.TB_NAME_USER_INFO, SELLING_INFO);
		sUriMatcher.addURI(AlbumPad.AUTHORITY,
				AlbumPad.TB_NAME_USER_INFO + "/#",
				ITEM_TB_NAME_APPLICATION_INFO);
		
		
		sUriMatcher.addURI(AlbumPad.AUTHORITY,
				AlbumPad.TB_NAME_PICINFO, PIC_INFO_d);
		sUriMatcher.addURI(AlbumPad.AUTHORITY,
				AlbumPad.TB_NAME_PICINFO + "/#",
				ITEM_TB_NAME__INFO);
		
		
	}

	void openDB() {
		try {
			db = mOpenHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = mOpenHelper.getReadableDatabase();
		}
	}

	@Override
	protected void finalize() throws Throwable {
		db.close();
		super.finalize();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		String tableName = uri.getPathSegments().get(0);
		// Validate the requested uri
		if (tableName == null) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		int count;

		// Set the table we're querying.
		qBuilder.setTables(tableName);

		// If the query ends in a specific record number, we're
		// being asked for a specific record, so set the
		// WHERE clause in our query.
		if ((sUriMatcher.match(uri)) >= 100) {
			// if ((sUriMatcher.match(uri)) >= 10) {
			qBuilder.appendWhere("_id=" + uri.getLastPathSegment());
		}

		count = db.delete(tableName, selection, selectionArgs);

		return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
		case SELLING_INFO:
			return AlbumPad.UserInfo.CONTENT_TYPE;
		case PIC_INFO:
			return AlbumPad.PicinfoList.CONTENT_TYPE;
		case PIC_INFO_d:
			return AlbumPad.Picinfo.CONTENT_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		String tableName = uri.getPathSegments().get(0);
		// Validate the requested uri
		if (tableName == null) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		if (values != null) {
			long rowId = db.insert(tableName, null, values);
			if (rowId > 0) {
				Uri itemUri = ContentUris.withAppendedId(uri, rowId);
				getContext().getContentResolver().notifyChange(itemUri, null);
				return itemUri;
			}
		}
		return null;

	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext());
		openDB();
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String tableName = uri.getPathSegments().get(0);
		// Validate the requested uri
		if (tableName == null) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		// Set the table we're querying.
		qBuilder.setTables(tableName);

		// If the query ends in a specific record number, we're
		// being asked for a specific record, so set the
		// WHERE clause in our query.
		if ((sUriMatcher.match(uri)) >= 100) {
			// if ((sUriMatcher.match(uri)) >= 10) {
			qBuilder.appendWhere("_id=" + uri.getLastPathSegment());
		}

		// Make the query.
 
		Cursor c = null;
        try {
        	 c = qBuilder.query(db, projection, selection, selectionArgs,
    				null, null, sortOrder);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		// Tell the cursor what uri to watch, so it knows when its source data
		// changes
		c.setNotificationUri(getContext().getContentResolver(), uri);
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		String tableName = uri.getPathSegments().get(0);
		// Validate the requested uri
		if (tableName == null) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		// Set the table we're querying.
		qBuilder.setTables(tableName);

		// If the query ends in a specific record number, we're
		// being asked for a specific record, so set the
		// WHERE clause in our query.
		if ((sUriMatcher.match(uri)) >= 100) {
			// if ((sUriMatcher.match(uri)) >= 10) {
			qBuilder.appendWhere("_id=" + uri.getLastPathSegment());
		}

		int count;

		count = db.update(tableName, values, selection, selectionArgs);

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			/**
			 * 创建 用户信息表
			 */
			db.execSQL("CREATE TABLE " + AlbumPad.TB_NAME_USER_INFO
					+ " (" + AlbumPad.PicinfoList.ID
					+ " INTEGER PRIMARY KEY," 
					+AlbumPad.UserInfo.itv_id+ " TEXT,"
					+AlbumPad.UserInfo.imsi+ " TEXT,"
					+AlbumPad.UserInfo.passport+ " TEXT,"
					+AlbumPad.UserInfo.mobile+ " TEXT,"
					+AlbumPad.UserInfo.nick+ " TEXT,"
			+AlbumPad.UserInfo.created+" TEXT,"
			+AlbumPad.UserInfo.vcode+" TEXT,"
			+AlbumPad.UserInfo.pause+" TEXT,"
			+AlbumPad.UserInfo.icon+" TEXT,"
			+AlbumPad.UserInfo.gender+" TEXT,"
			+AlbumPad.UserInfo.last_time+" TEXT,"
			+AlbumPad.UserInfo.status+" TEXT );");
			/**
			 * 创建 相册属性表
			 */
			db.execSQL("CREATE TABLE " + AlbumPad.TB_NAME_PICINFOLIST_INFO
					+ " (" + AlbumPad.PicinfoList.ID
					+ " INTEGER PRIMARY KEY," 
					+AlbumPad.PicinfoList.fileid+ " TEXT,"
					+AlbumPad.PicinfoList.picid+ " TEXT,"
					+AlbumPad.PicinfoList.passport+ " TEXT,"
					+AlbumPad.PicinfoList.public_+ " TEXT,"
					+AlbumPad.PicinfoList.typeid+ " TEXT,"
					+AlbumPad.PicinfoList.upload_time+ " TEXT,"
			+AlbumPad.PicinfoList.title+" TEXT,"
			+AlbumPad.PicinfoList.content+" TEXT,"
			+AlbumPad.PicinfoList.views+" TEXT,"
			+AlbumPad.PicinfoList.score+" TEXT,"
			+AlbumPad.PicinfoList.block+" TEXT,"
			+AlbumPad.PicinfoList.area+" TEXT,"
			+AlbumPad.PicinfoList.coordinate+" TEXT,"
			+AlbumPad.PicinfoList.hash_code+" TEXT,"
			+AlbumPad.PicinfoList.path_image+" TEXT,"
			+AlbumPad.PicinfoList.exif+" TEXT );");
			
			/**
			 * 创建 单张图片属性表
			 */
			db.execSQL("CREATE TABLE " + AlbumPad.TB_NAME_PICINFO
					+ " (" + AlbumPad.Picinfo.ID
					+ " INTEGER PRIMARY KEY," 
					+AlbumPad.Picinfo.fileid+ " TEXT,"
					+AlbumPad.Picinfo.passport+ " TEXT,"
					+AlbumPad.Picinfo.public1+ " TEXT,"
					+AlbumPad.Picinfo.typeid+ " TEXT,"
					+AlbumPad.Picinfo.upload_time+ " TEXT,"
			+AlbumPad.Picinfo.title+" TEXT,"
			+AlbumPad.Picinfo.content+" TEXT,"
			+AlbumPad.Picinfo.views+" TEXT,"
			+AlbumPad.Picinfo.score+" TEXT,"
			+AlbumPad.Picinfo.block+" TEXT,"
			+AlbumPad.Picinfo.area+" TEXT,"
			+AlbumPad.Picinfo.coordinate+" TEXT,"
			+AlbumPad.Picinfo.hash_code+" TEXT,"
			+AlbumPad.Picinfo.path_image+" TEXT,"
			+AlbumPad.Picinfo.small+" TEXT,"
			+AlbumPad.Picinfo.big+" TEXT,"
			+AlbumPad.Picinfo.publicDesc+" TEXT,"
			+AlbumPad.Picinfo.exif+" TEXT );");
			 
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
			db.execSQL("DROP TABLE IF EXISTS "
					+ AlbumPad.TB_NAME_USER_INFO);

		}

	}
	public  static void closeDB(){
		if(db != null){
			db.close();
		}
	}

}
